package chapter1_11_threadlocal;

/**
 *
 * Threadlocal 不支持继承性
 *
 *
 * 当父线程创建子线程时，构造函数会把父线程中inheritableThreadLocals 变量里面的本地变量
 * 复制一份保存到子线程的inheritableThreadLocals 变量里面。
 * @author huayuhao
 * @date 2020/12/16 14:03
 */
public class TestThreadLocal {

    static ThreadLocal<String> local  = new ThreadLocal<>();
    static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        // 主线程设置
        local.set("hello world");
        Thread thread = new Thread(()->{
            // 子线程输出
            System.out.println("thread : " + local.get());
        });

        thread.start();
        thread.join();
        // 主线程输出
        System.out.println("main : " + local.get());

        System.out.println("======接下来测试inheritableThreadLocal=======");

        inheritableThreadLocal.set("hello world");
        Thread t = new Thread(()->{
            System.out.println("t : " + inheritableThreadLocal.get());
        });

        t.start();
        t.join();
        System.out.println("main : " + inheritableThreadLocal.get());
    }
}
